unit UEditNPC;

interface

uses
  Windows, SysUtils, Classes, Graphics, Controls, Forms, Math,
  Dialogs, StdCtrls, Buttons, ComCtrls, ExtDlgs, TypInfo,
// selfmade units
  UConfig, UNPC, UMapCommon, uUtils;

type


  TFrmEditNPC = class(TForm)
    BtnCancel: TBitBtn;
    BtnOK: TBitBtn;
    DLImgOpen: TOpenPictureDialog;
    DLOpen: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure FNameDblClick(Sender: TObject);
    procedure BtnOKClick(Sender: TObject);
  private
    CurNPCId: Integer;
    NpcControls: array[NNpcParam] of THackControl;
  public
    function Add(): TModalResult;
    function Edit(Index: Integer): TModalResult;
    procedure WanderModelChange(Sender: TObject);
    procedure WanderPointDblClick(Sender: TObject);
  end;

var
  FrmEditNPC: TFrmEditNPC;

implementation

uses
  USigns;

{$R *.dfm}

{ TFrmEditNPC }

function TFrmEditNPC.Add(): TModalResult;
begin
  Result := Edit(NPCConfig.Add);
end;

procedure TFrmEditNPC.BtnOKClick(Sender: TObject);
var
  i: NNpcParam;
  j : Integer;
  LV: TOrderSetCtrl;
  wpnum : string;
begin
  with NPCConfig do
    for i := Low(NNpcParam) to High(NNpcParam) do
      if (i = npWanderPoints) then
        if IsSpecWanderModel(Items[CurNpcId, npWanderModel]) then
        begin
          LV := TOrderSetCtrl(NpcControls[i]);
            for J := 0 To LV.Items.Count - 1 do
            begin
              wpnum := LV.Items[j].Caption;
                if wpnum <> '0' then
                  Write(Format('%d%s%s%s', [CurNpcId, Delim, 'Point', wpnum]),
                    LV.Items[j].SubItems[0]);
            end;
        end
        else
      else
        Items[CurNpcId, i] := NpcControls[i].Text;
end;

function TFrmEditNPC.Edit(Index: Integer): TModalResult;
var
  i: NNpcParam;
  LV: TOrderSetCtrl;
  j, w : Integer;
begin
  FillWithList(NpcControls[npwanderpoints], SignConfig);
  CurNpcId := Index;
  LV := TOrderSetCtrl(NpcControls[npwanderpoints]);
  LV.SortType := stNone;
  for i := Low(NNpcParam) to High(NNpcParam) do
    if i = npWanderPoints then
    begin
      for J := 0 To LV.Items.Count - 1 do
        if TryStrToInt(NPCConfig.Read(Format('%d%s%s%d', [CurNpcId,
          NPCConfig.Delim, 'Point', j + 1])) , w) then
          LV.Items[w - 1].Caption := IntToStr(j + 1);
      for J := 0 To LV.Items.Count - 1 do
        if LV.Items[j].Caption = '' then
          LV.Items[j].Caption := '0';
    end
    else
      NpcControls[i].text := NPCConfig.Items[Index, i];
  LV.SortType := stText;
  WanderModelChange(nil);
  Caption := Format('Editing NPC #%d', [CurNpcId]);
  Result := ShowModal;
end;

procedure TFrmEditNPC.FNameDblClick(Sender: TObject);
var
  Ctrl: THackControl absolute Sender;
  DL : TOpenDialog;
begin
  DL := nil;
  if NNpcParam(Ctrl.Tag) = npScript then
  begin
    DL := DLOpen;
    DL.InitialDir := Path + 'data\Scripts\quests';
  end;
  if NNpcParam(Ctrl.Tag) = npPortrait then
  begin
    DL := DLImgOpen;
    DL.InitialDir := Path + 'data\Images\portrets';
  end;
  if DL.Execute then
    Ctrl.Text := ExtractFileName(DL.FileName);
end;

procedure TFrmEditNPC.FormCreate(Sender: TObject);
var
  i: NNpcParam;
  ATop: Integer;
begin
  ATop := 0;
  for i := Low(NNpcParam) to High(NNpcParam) do
  begin
    TControl(NpcControls[i]) := ControlTypes[NpcParamType[i]].Create(Self);
    if not (i in [npWanderPoints]) then
    with TLabel.Create(Self) do
    begin
      Top := ATop;
      Parent := Self;
      Caption := NpcParamNames[i];
    end;
    with NpcControls[i] do
    begin
      Top := ATop;
      Left := 100;
      Tag := Ord(i);
      Parent := Self;
//      NpcControls[i].OnTextChanged := nil;
      if i in [npID] then
        Enabled := False;
      if NpcParamType[i] in [npFName, npImgFName] then
        NpcControls[i].OnDblClick := FNameDblClick;

      if i in [npWanderModel] then
      begin
        FillWithEnum(NpcControls[i], TypeInfo(NWanderModel),
          Point(Ord(Low(NWanderModel)), Ord(High(NWanderModel))));
        TListCtrl(NpcControls[i]).OnChange := WanderModelChange;
//        NpcControls[i].OnTextChanged := WanderModelTextChange;
      end;
      if i in [npObjImgID] then
        FillWithImages(NpcControls[i], path + 'data\images\' + NPCConfig.GetBMPsFName,
          TransCol);
      if i in [npWanderPoints] then
      begin
        Top := 0;
        Left := 250;
        Parent := Self;
        Width := Self.ClientWidth - Left;
        Height := Self.ClientHeight;
        Anchors := [akleft, akTop, akRight, akBottom];
        Caption := NpcParamNames[i];
        OnDblClick := WanderPointDblClick;
      end;
    end;
    Inc(ATop, NpcControls[i].Height + 4);
  end;
end;

procedure TFrmEditNPC.WanderModelChange(Sender: TObject);
var
  str: string;
begin
  str := NpcControls[npWanderModel].Text;
  NpcControls[npWanderPoints].Enabled := IsSpecWanderModel(str);
end;

procedure TFrmEditNPC.WanderPointDblClick(Sender: TObject);
var
  LV: TOrderSetCtrl;
  itm : TListItem;
  i : Integer;
  mx : Integer;
begin
  LV := TOrderSetCtrl(NpcControls[npWanderPoints]);
  mx := StrToInt(LV.Items[0].Caption);
 //  Combo.ItemIndex := Combo.Items.IndexOf(NpcControls[npWanderModel].Text);
  itm := LV.Items[LV.ItemIndex];
  if Assigned(itm) then
  if itm.Caption <> '0' then
  begin
    LV.Items.BeginUpdate;
    itm.Caption := '0';
    for i := 0 To LV.Items.Count - 1 do
      if LV.Items[i].Caption <> '0' then
        LV.Items[i].Caption := IntToStr(StrToInt(LV.Items[i].Caption) - 1);
    LV.Items.EndUpdate;
  end
  else
  begin
    LV.Items.BeginUpdate;
    for i := 0 To LV.Items.Count - 1 do
      if LV.Items[i].Caption <> '0' then
        mx := Max(mx, StrToInt(LV.Items[i].Caption));
    itm.Caption := IntToStr(mx + 1);
    LV.Items.EndUpdate;
  end; 
end;

end.

